package com.driving.single.common.coach.service.impl;

import com.driving.single.common.coach.service.CommonCoachCommentService;
import com.driving.single.common.domain.TbCoachComment;
import com.driving.single.common.entity.PageResult;
import com.driving.single.common.mapper.TbCoachCommentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class CommonCoachCommentServiceImpl implements CommonCoachCommentService {
    @Autowired
    private TbCoachCommentMapper tbCoachCommentMapper;

    /**
     * 根据主键查询驾校评论
     *
     * @param id 主键
     */
    @Override
    public TbCoachComment selectTbCoachCommentById(Integer id) {
        return this.tbCoachCommentMapper.selectByPrimaryKey(id);
    }

    /**
     * 选取评分最高的评论
     *
     * @param coachId 教练ID
     */
    @Override
    public TbCoachComment selectBestTbCoachCommentByCoachId(Long coachId) {
        Example example = new Example(TbCoachComment.class);
        example.setOrderByClause("`total_score` DESC");
        example.createCriteria().andEqualTo("coachId", coachId);
        List<TbCoachComment> tbCoachCommentList = this.tbCoachCommentMapper.selectByExample(example);
        if (tbCoachCommentList != null && tbCoachCommentList.size() > 0) {
            return tbCoachCommentList.get(0);
        }
        return null;
    }

    /**
     * 实现根据教练ID   分页查询教练评论   接口
     *
     * @param coachId 教练主键
     * @param page     当前页
     * @param rows     页显示数
     */
    @Override
    public PageResult selectCoachCommentByCoachIdWithPage(Long coachId, Integer page, Integer rows) {
        PageHelper.startPage(page, rows);

        Example example = new Example(TbCoachComment.class);
        //  评论时间排序 最新时间排序  ASC
        example.setOrderByClause("`created` DESC");
        example.createCriteria().andEqualTo("coachId", coachId);
        List<TbCoachComment> list = this.tbCoachCommentMapper.selectByExample(example);

        PageInfo<TbCoachComment> pageInfo = new PageInfo<>(list);

        PageResult pageResult = new PageResult();
        pageResult.setResult(list);
        pageResult.setPageIndex(page);
        pageResult.setTotalSize(pageInfo.getTotal());
        pageResult.setTotalPage(pageInfo.getPages());

        return pageResult;
    }
}
